package org.czl.leetCode1;

public class l902 {
    public int atMostNGivenDigitSet(String[] digits, int num) {
        int n = num/10;
        int offset = 1;
        while(n > 0){
            n /= 10;
            offset *= 10;
        }
        int[] digit = new int[digits.length];
        for(int i = 0; i < digits.length; i++){
            digit[i] = Integer.parseInt(digits[i]);
        }
        return f(digit, offset, num, 0,0);
    }
    int f(int[] digit, int offset, int num, int free, int fix){
        if(offset == 0){
            return fix == 1 ? 1 : 0;
        }
        int ans = 0;
        if(fix == 0){
            ans += f(digit, offset/10, num, 1, 0);
        }
        int cur = (num/offset) % 10;
        if(free == 0){
            for(int i : digit){
                if(i < cur){
                    ans += f(digit,  offset/10, num, 1, 1);
                }else if(cur == i){
                    ans += f(digit,  offset/10, num, 0, 1);
                }else{
                    break;
                }
            }
        }else{
            ans += f(digit, offset/10, num, 1, 1) * digit.length;
        }
        return ans;
    }
}
